<?php

require_once('database.php');
if (!isset($_SESSION))
			session_start();
class user
{
	public static function get_question ( $exceptions, $category )		// returns false if there's no other question to answer
	{
		$exceptions 	= isset($exceptions) && is_array($exceptions) ? $exceptions : array ();
		$questions 		= user::questions_num($category);
		$answers		= user::get_answered($category);
		
		$answers_len 	= count ( $answers );
		$exceptions_len = count ( $exceptions );
		
		if ( $answers_len + $exceptions_len >= $questions )
		{
			if ( $answers_len >= $questions ) return false;
			
			$exceptions 		= array ();
			$exceptions_len		= 0;
			$exceptions_reset	= true;
		}
		
		$question	= false;
		$db 		= database::get_instance();
		while (!$question)
		{
			$r		= $db->query(	
							"	SELECT *
								FROM `".$category."_questions`
								WHERE `id` NOT IN (SELECT `question_id`
												   FROM ".$category."_answers
											   	   WHERE username = '".$_COOKIE['username']."')
								ORDER BY RAND()
								LIMIT 1 " );
			$r		= $db->fetch( $r );
			
			if (	! in_array ( $r['id'], $answers ) &&
				 	! in_array ( $r['id'], $exceptions )
				) 
					$question = $r;
		}
		
		$question['reset_exceptions'] = $exceptions_reset ? 'true' : 'false';
		
		return $question;
	}

	public static function questions_num ($category)
	{
		$db = database::get_instance();
		$r	= $db->query(
				'	SELECT *
					FROM `'.$category.'_questions` ' );
		
		return $r ? mysql_num_rows($r) : 0;
	}

	public static function get_answered ($category)		//returns answered id's
	{
		$db = database::get_instance();
		
		$r	= $db->query(
					'	SELECT `question_id`
						FROM `'.$category.'_answers`
						WHERE username = \''.$_COOKIE['username'].'\' ' );
		
		if ( ! $r ) return false;
		
		$qid = array ();
		while ($row = $db->fetch( $r )) array_push ( $qid, $row[0] );
		
		return $qid;
	}

	public static function get_answers ($category)		//returns all answered questions by this user
	{
		$db = database::get_instance();
		
		$r	= $db->query(
					'	SELECT *
						FROM `'.$category.'_answers`
						WHERE `username` = \''.$_COOKIE['username'].'\' ' );
						
		$r	= $db->to_array( $r );
		// if (!isset($_COOKIE['username']))
		// 	return array("12","2");
		
		return $r;
	}

	// public static function set_answer ( $category, $question_id, $answer_num )
	// {
	// 	if ( 	($answer_num < 1) ||
	// 			($answer_num > 4)
	// 		)
	// 			return;
	// 	
	// 	$db 	= database::get_instance();
	// 	//check if question exists
	// 	$qid 	= $db->get_field_value( $category.'_questions', 'question', '`id`='.$question_id );
	// 	if ( !$qid ) { return false; }
	// 	
	// 	//checks if question if answered
	// 	$ans	= $db->get_field_value( $category.'_answers', 'answer', '`username`=\''.$_COOKIE['username'].'\' AND `question_id`='.$question_id );
	// 	if ( $ans !== false ) { return false; }
	// 	
	// 	//set answer
	// 	$r		= $db->query(
	// 					'	INSERT
	// 						INTO `'.$category.'_answers`
	// 						VALUES ( '.$question_id.', \''.$_COOKIE['username'].'\', '.$answer_num.' )' );
	// 	
	// 	return $r ? true : false;
	// }

	public static function set_answer( $category, $question_id, $answer_num )
	{
		if ( 	($answer_num < 1) ||
				($answer_num > 4)
			)
				return;
		
		$db 	= database::get_instance();
		//check if question exists
		$qid 	= $db->get_field_value( $category.'_questions', 'question', '`id`='.$question_id );
		if ( !$qid ) { return false; }
		
		//checks if question if answered
		$ans	= $db->get_field_value( $category.'_answers', 'answer', '`username`=\''.$_SESSION['username'].'\' AND `question_id`='.$question_id );
		if ( $ans !== false ) { return false; }
		
		$r		= $db->query(
						'	INSERT
							INTO `'.$category.'_answers`
							VALUES ( '.$question_id.', \''.$_SESSION['username'].'\', '.$answer_num.' )' );
		return $r ? true : false;
	}
}

?>
